/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.bean;

import com.sunrise.boundary.InvitationRequestManager;
import com.sunrise.boundary.UserManager;
import com.sunrise.boundary.WeatherAlertManager;
import com.sunrise.entity.Event;
import com.sunrise.entity.InvitationRequest;
import com.sunrise.entity.User;
import com.sunrise.entity.WeatherAlert;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author frank
 */
@Named(value = "notificationsBean")
@Dependent
public class NotificationsBean {
    
    @EJB
    InvitationRequestManager invReqManager;
    
    @EJB
    WeatherAlertManager waManager;
    
    @EJB
    UserManager um;
    
    private final SimpleDateFormat dateFormatter = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);
    
    public List<InvitationRequest> getInvitations() {
        Map params = new HashMap<>();
        params.put("recipient", um.getLoggedUser());
        
        return invReqManager.findManyWithNamedQuery(InvitationRequest.findByRecipient, params);
    }
    
    public List<WeatherAlert> getWeatherAlerts() {
        User recipient = um.getLoggedUser();
        return waManager.findWeatherAlertsForRecipient(recipient);
    }
    
    public String acceptInvitation(InvitationRequest req) {
        req.setViewed(true);
        req.setAccepted(true);
        invReqManager.update(req);
        return "/core/calendar?faces-redirect=true";
    }
    
    public String declineInvitation(InvitationRequest req) {
        if (req != null) {            
            req.setViewed(true);
            req.setAccepted(false);
            invReqManager.update(req);
        }
        
        return "/core/calendar?faces-redirect=true";
    }
    
    public String declineInvitationForEvent(Event e) {
        InvitationRequest r = invReqManager.findInvitationRequest(e, um.getLoggedUser());
        return this.declineInvitation(r);
    }

    public String markAsViewedWeatherAlert(WeatherAlert wa) {
        wa.setViewed(true);
        waManager.update(wa);
        
//        RequestContext context = RequestContext.getCurrentInstance();
//        context.update("Notifications");
//        context.update("abcbsdsbfds");
//        context.execute("PF('myschedule').update();PF('eventDialog').hide();");
        return "/core/calendar?faces-redirect=true";
    }
    
    public void openEventForRescheduling(WeatherAlert wa) {
        wa.setViewed(true);
        waManager.update(wa);
        
        //RequestContext context = RequestContext.getCurrentInstance();
//        context.update("Notifications");
        //context.update("openNotifButton");

        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); 
        try {
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("rescheduleEvent", wa.getEvent());
            context.redirect("calendar.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(UserSearchBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
//        context.execute("PF('myschedule').update();PF('eventDialog').hide();");
    }
    
    public String formatDate(Date date) {
        return dateFormatter.format(date);
    }
    
    public String getNotificationsCount() {
        int count = this.getInvitations().size() + this.getWeatherAlerts().size();
        if (count == 0) {
            return "";
        } else {
            return "("+Integer.toString(count)+")";
        }
    }
}
